<!-- 首页 -->
<template>
	<div id="index_main" @touchstart="hidepupop">
		<u-sticky offsetTop="-20">
			<view id="search_ceiling">
				<view class="search_left" @click="gotosearch()">
					<u-search :show-action="false" :clearabled="false" shape="round" bg-color="#fff" :disabled="true">
					</u-search>
				</view>
				<view class="search_right">
					<u-icon :name='icons.fabu' size="60" margin-top="5" style="margin: 10rpx auto;"
						@click="pupopshowchange()">
					</u-icon>
					<view class="pupop" v-if="showpupop==1">
						<view class="pupopo-button" @touchstart="sendverify(0)">
							<u-icon :name="icons.fabuxuqiu" size="48" label="发布需求" label-pos="right" label-color="#000"
								label-size="24" margin-top="2" style="margin-left: 16rpx;margin-top: 8rpx;">
							</u-icon>
						</view>
						<u-line color="rgb(56, 56, 56)" />
						<view class="pupopo-button" @touchstart="sendverify(1)">
							<u-icon :name="icons.fabushangpin" size="48" label="发布商品" label-pos="right"
								label-color="#000" label-size="24" margin-top="2"
								style="margin-left: 16rpx;margin-top: 8rpx;">
							</u-icon>
						</view>
					</view>
					<view v-if="showpupop==0">
					</view>
				</view>
			</view>
		</u-sticky>
		<view id="index_body">
			<view id="index_swiper">
				<u-swiper :list="swiper_list" height="354" border-radius="10" @click="card()"></u-swiper>
			</view>

			<div id="index_subsection">
				<!-- 分割器 -->
				<u-subsection :list="subsection_list" :current="curNum" buttonColor="#ff673d" bg-color="#e8e8e8"
					active-color="#ffffff" inactive-color="#808080" height="78" @change="sectionChange"></u-subsection>
			</div>

			<div class="icard" v-if='curNum==0'>
				<!-- 商品 瀑布流 -->
				<waterfall :datalist="flowList"></waterfall>
			</div>
			<div class="icard" v-if='curNum==1'>
				<waterfall :datalist="prealseList"></waterfall>
			</div>
			<u-divider v-if="bottom" color="#878787" bg-color="#f5f5f5" margin-top="60" margin-bottom="60"
				@click="getData()">到底了
			</u-divider>
			<u-back-top :scroll-top="scrollTop" mode="square" tips="顶部"></u-back-top>
		</view>
	</div>
</template>

<script>
	import waterfall from '../components/waterfall/waterfall.vue'
	import {
		iconurl
	} from '../../uti/api.js'
	export default {
		components: {
			waterfall
		},
		data() {
			return {
				icons: {
					fabu: `${iconurl}/img/fabu.svg`,
					fabuxuqiu: `${iconurl}/img/fabuxuqiu.svg`,
					fabushangpin: `${iconurl}/img/fabushangqin.svg`,
				},
				bottom: false,
				getdataindexflow: 0,
				getdataindexprealse: 0,
				showpupop: 0,
				userstatus: -1, //用户状态
				// 轮播图

				swiper_list: [{
						image: 'http://49.235.104.90:1013/down/k6KIgx6DCK6B',
						title: " "
					},

					{
						image: 'http://49.235.104.90:1013/down/YEJnGuqxhnH5',
						title: " "
					},
					{
						image: 'http://49.235.104.90:1013/down/t2WxwfPvU06N',
						title: " "
					}
				],
				// 分类
				subsection_list: [{
						name: '在售' //0
					},
					{
						name: '预售' //1
					}
				],
				// 分类按钮状态
				curNum: 0,

				//回到顶部
				scrollTop: 0,

				//商品卡片
				// loadStatus: 'loadmore',
				flowList: [],
				prealseList: []
			}
		},

		onReachBottom() { //到达底部
			// str = "C:\Users\zhaoj\WeChatProjects\miniprogram-2"
			this.getData();
		},
		methods: {
			icon(url) {
				return iconurl + url;
			},
			//发布验证
			sendverify(Authority) {
				let opt = this.userstatus;
				let url = (Authority == 0 ? '../subpages/sends/sends_need' : '../subpages/sends/sends_goods');
				this.goto(url)
				// if(opt>=Authority){
				// 	this.goto(url)
				// }else{
				// 	uni.showToast({
				// 		title:'请先完成身份/资质认证',
				// 		icon:'error',
				// 		duration:3000
				// 	})
				// }
			},

			hidepupop() {
				this.showpupop = 0;
			},
			pupopshowchange() {
				this.showpupop = 1 //(this.showpupop+1)%2;
			},
			sectionChange(index) {
				this.curNum = index;
				if (this.flowList.length == 0 || this.prealseList.length == 0) {
					this.getData();
				}

			},
			addRandomData(list) {
				for (let i = 0; i < list.length; i++) {
					if (list[i].goodsStatus == 0) {
						this.flowList.push(list[i]);
					} else {
						this.prealseList.push(list[i]);
					}
				}

			},
			card() {
				console.log("test");
				console.log(this.swiper_list);
			},
			//页面跳转
			goto(url) {
				uni.navigateTo({
					url: url,
					animationType: 'slide-in-right',
					animationDuration: 1000

				})
			},
			gotosearch() {
				uni.navigateTo({
					url: '../subpages/search/search',
					animationType: 'fade-in',
					animationDuration: 500

				})
			},
			getuserinfo() {
				this.$myRequest({
					url: '/receivingAddress/getSelfAddress',
					method: 'POST'
				}).then((res) => {
					// console.log('res',res);
					if (res.statusCode == 200) {
						console.log('success');
						getApp().globalData.userinfo = res.data.data[0];
						// console.log(getApp().globalData.userinfo)
						this.userstatus = getApp().globalData.userinfo.userStatus
					} else {
						uni.showToast({
							title: '获取用户信息失败',
							icon: 'error',
							duration: 2000
						})
					}
				})
			},
			async getData() {
				let index = this.curNum;
				let iurl = '/goods/getIndexGoods';
				let pagenum;
				index == 0 ? pagenum = this.getdataindexflow : pagenum = this.getdataindexprealse;
				let pageSize = pagenum > 0 ? 8 : 10;
				// pagenum = parseInt(pagenum);
				const res = await this.$myRequest({
					method: 'POST',
					url: iurl,
					data: {
						pageNum: pagenum,
						pageSize: pageSize,
						status: index,
					}
				}).then((res) => {
					// console.log(res);
					if (res.data.code === 200) {
						this.addRandomData(res.data.data);
						if (res.data.data.length === 0) {
							this.bottom = true;
						}
						index == 0 ? this.getdataindexflow += 1 : this.getdataindexprealse += 1;
					} else {
						uni.showToast({
							title: '数据获取失败',
							icon: 'error',
							duration: 2000
						})
					}
				})
				// console.log(res);

			}
		},
		onLoad() {
			// console.log('123', this.icon_Combined_Shape)
			this.getData(); //请求商品数据
			setTimeout(() => {
				this.getuserinfo();
			}, 1000)

		},
		onPullDownRefresh() {
			//监听下拉刷新动作的执行方法，每次手动下拉刷新都会执行一次
			this.bottom = false;
			this.getdataindexflow = 0;
			this.getdataindexprealse = 0;
			this.showpupop = 0;
			this.userstatus = -1;
			this.flowList = [];
			this.prealseList = [];
			this.getData();
			this.getuserinfo();
			setTimeout(function() {
				uni.stopPullDownRefresh(); //停止下拉刷新动画
			}, 500);
		},
		onPageScroll(e) { //生命周期函数
			this.scrollTop = e.scrollTop;
		}
	}
</script>
<style>
	@import url("index.css");
</style>
